package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.RecommendResponse;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
/**
 * 补注释
 * Author:lhm
 * @return
 * @since 2022/07/11 17:35
 */
@RestController
public class ProductDetailController {


    @Autowired
    IProductService iProductService;
    /**
     * 查看商品详情 
     * @param id
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @since 2022/07/11 17:32
     */

    @GetMapping("/shopping/product/{id}")
    public ResponseData getProductDetail(@PathVariable("id") Long id) {
        //用request封装请求参数
        ProductDetailRequest request = new ProductDetailRequest();
        request.setId(id);
        //用response封装返回值
        ProductDetailResponse response= iProductService.getProductDetail(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            //响应response中的dto
            return new ResponseUtil().setData(response.getProductDetailDto(),"success");
        }

        // 执行失败
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
    /**
     * 分页查询商品列表
     * @param request 
     * @return com.cskaoyan.mall.commons.result.ResponseData 
     * @since 2022/07/11 17:33
     */
    @GetMapping("/shopping/goods")
    public ResponseData getAllProduct(AllProductRequest request){
        //用request封装参数
        AllProductResponse response=iProductService.getAllProduct(request);
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            PageResponse pageResponse = new PageResponse();
            pageResponse.setData(response.getProductDtoList());
            pageResponse.setTotal(response.getTotal());
            return  new ResponseUtil().setData(pageResponse,"success");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
    /**
     * 查询推荐商品
     * @return com.cskaoyan.mall.commons.result.ResponseData 
     * @since 2022/07/11 17:34
     */
    @GetMapping("/shopping/recommend")
    public ResponseData getRecommendGoods(){
        RecommendResponse response = iProductService.getRecommendGoods();
        if (ShoppingRetCode.SUCCESS.getCode().equals(response.getCode())) {
            // 执行成功
            return  new ResponseUtil().setData(response.getPanelContentItemDtos(),"success");
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

}
